//
//  SpecialLineViewController.m
//  Commerce
//
//  Created by xiaofeifei on 15/2/16.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "SpecialLineViewController.h"
#import "SpeclalLineListViewController.h"
#import "AreaViewController.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>

@interface SpecialLineViewController ()<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
    NSDictionary *_startingDic;
    NSDictionary *_destinationDic;
    BMKLocationService* _locService;
    BMKGeoCodeSearch* _geocodesearch;
}

@end

@implementation SpecialLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    _geocodesearch.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [self startLocation];
    // Do any additional setup after loading the view from its nib.
}


#pragma mark -IBAction
- (IBAction)clickSearch:(id)sender{
    if (_startingField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请选择出发地"];
        return;
    }
    if (_destinationField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请选择目的地"];
        return;
    }
    SpeclalLineListViewController *vc = [[SpeclalLineListViewController alloc] initWithNibName:nil bundle:nil];
    vc.navStr = @"搜索地址";
    vc.startingDic = _startingDic;
    vc.destinationDic = _destinationDic;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)gotoStartingView:(id)sender{
    AreaViewController *vc = [[AreaViewController alloc] initWithNibName:nil bundle:nil];
    vc.typeNum = 1;
    vc.delegate =self;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)gotoDestinationView:(id)sender{
    AreaViewController *vc = [[AreaViewController alloc] initWithNibName:nil bundle:nil];
    vc.typeNum = 2;
    vc.delegate =self;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -AreaDelegate
- (void)showAreaData:(NSInteger)TypeNum andArea:(NSDictionary *)areaDic andAreaName:(NSString *)areaName{
    if (TypeNum == 1) {
        _startingDic = areaDic;
        _startingField.text = areaName;
    }else if (TypeNum == 2){
        _destinationDic = areaDic;
        _destinationField.text = areaName;
    }
}


- (void)startLocation{
    //启动LocationService
    [_locService startUserLocationService];
}

#pragma mark -
#pragma mark loction
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_locService stopUserLocationService];
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    
    [[GlobalData sharedGlobalData] setUselocation:userLocation.location.coordinate];
    
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
    }
    else
    {
        NSLog(@"反geo检索发送失败");
    }
    
}

/**
 *在地图View停止定位后，会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
    NSLog(@"stop locate");
}

/**
 *定位失败后，会调用此函数
 *@param mapView 地图View
 *@param error 错误号，参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}


-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == 0) {
        _startingField.text = result.addressDetail.city;
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"AreaData"];
        if (data != nil) {
            NSError *error = nil;
            
            id dataAry = [NSJSONSerialization
                          
                          JSONObjectWithData:data
                          
                          options:NSJSONReadingMutableLeaves
                          
                          error:&error];
            for (NSDictionary *item in dataAry) {
                if ([result.addressDetail.province hasPrefix:item[@"AreaName"]]) {
                    for (NSDictionary *dic in item[@"CityList"]) {
                        if ([result.addressDetail.city hasPrefix:dic[@"AreaName"]]) {
                            _startingDic = dic;
                            break;
                        }
                    }
                }
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
